#######################################
#Relative Capture: Quasi-Experimental Evidence from the Chinese Judiciary#
#R Plots#
#Yuhua Wang#
######################################


library(foreign) 
library(ggplot2) 
require(ggplot2)

###############
#Scatter Plot (Figure 2 in Paper)#
##############

data <- read.dta("/Users/ywang/Documents/My Documents/Research/working papers/political connections and courts/data/working/CPS relative capture datafile/figures/data/allcases_Guangzhou.dta")

attach(data)

data$level <- as.factor(data$level)

ggplot(data, aes(x=acceptanceyear, y=stake_million,shape=level, color=level)) +
  geom_point(aes(size = 3)) +
  scale_x_continuous(name="Year", breaks=c(1997,2000, 2005, 2008,2010,2013)) +
  scale_y_continuous(name="Case Claim (Million Yuan)",breaks=c(0,6, 50, 100,150))+
  geom_vline(xintercept = 2008, linetype=2)+
  geom_segment(aes(x=1997, xend=2008, y=6, yend=6), linetype=1,size=1) + 
  geom_segment(aes(x=1997, xend=2008, y=100, yend=100), linetype=1,size=1) + 
  geom_segment(aes(x=2008, xend=2013, y=50, yend=50), linetype=1,size=1) +
  geom_segment(aes(x=1997, xend=2008, y=50, yend=50), linetype=2) + 
  geom_segment(aes(x=2008, xend=2013, y=6, yend=6), linetype=2) + 
  geom_segment(aes(x=2008, xend=2013, y=100, yend=100), linetype=2) +
  
  annotate("text", x = 1999, y=10, label = "Intermediate Court Cutoff")+
  annotate("text", x = 1998, y=104, label = "High Court Cutoff")+
  annotate("text", x = 2011, y=54, label = "Intermediate Court Cutoff")+
  annotate("text", x = 2000, y=25, label = "Cell 3")+
  annotate("text", x = 2000, y=-5, label = "Cell 1")+
  annotate("text", x = 2000, y=75, label = "Cell 5")+
  annotate("text", x = 2011, y=25, label = "Cell 4")+
  annotate("text", x = 2011, y=-5, label = "Cell 2")+
  annotate("text", x = 2011, y=75, label = "Cell 6")+
  scale_shape_discrete(name  ="Court Level",
                       breaks=c("1", "2", "3"),
                       labels=c("Basic", "Intermediate", "High"))+
  
  scale_color_discrete(name  ="Court Level",
                       breaks=c("1", "2", "3"),
                       labels=c("Basic", "Intermediate", "High"))



###############
#DID Plot (Figure 3 in Paper)#
##############


data <- read.dta("/Users/ywang/Documents/My Documents/Research/working papers/political connections and courts/data/working/CPS relative capture datafile/figures/data/did.dta")

attach(data)

data$treatment <- as.factor(data$treatment)

ggplot(data=data, aes(x=newregime, y=soe_win,group=treatment,shape=treatment,color=treatment)) + 
  geom_line(aes(linetype=treatment), size=1) + 
  scale_linetype_manual(values=c("solid", "dashed", "dotted"))+
  scale_color_manual(values=c("red",  "black", "black"))+
  geom_point(size=1,fill="red")+
  scale_x_discrete(name="", limits=c("Pre-2008","Post-2008")) +
  scale_y_continuous(name="Win Rate (%)",limits=c(20,80), breaks=c(20,30,40,50,60,70,80))+
  ggtitle("SOEs")+ 
  theme(legend.position="none")+
  annotate("text", x = "Post-2008", y=44, label = "Treatment Group")+
  annotate("text", x = "Post-2008", y=51, label = "Control Group")+
  annotate("text", x = "Post-2008", y=58, label = "Parallel Trend of Control Group")

ggplot(data=data, aes(x=newregime, y=pri_win,group=treatment,shape=treatment,color=treatment)) + 
  geom_line(aes(linetype=treatment), size=1) + 
  scale_linetype_manual(values=c("solid", "dashed", "dotted"))+
  scale_color_manual(values=c("red",  "black", "black"))+
  geom_point(size=1,fill="red")+
  scale_x_discrete(name="", limits=c("Pre-2008","Post-2008")) +
  scale_y_continuous(name="Win Rate (%)",limits=c(20,80), breaks=c(20,30,40,50,60,70,80))+
  ggtitle("Non-SOEs")+ 
  theme(legend.position="none")+
  annotate("text", x = "Post-2008", y=58, label = "Treatment Group")+
  annotate("text", x = "Post-2008", y=41, label = "Control Group")+
  annotate("text", x = "Post-2008", y=28, label = "Parallel Trend of Control Group")


###############
#DID Yearly Lines (Figure 2.1 in Appendix)#
##############

soe <- read.dta("/Users/ywang/Documents/My Documents/Research/working papers/political connections and courts/data/working/CPS relative capture datafile/figures/data/did_yearly_soe.dta")
attach(soe)
names(soe)

plot1 <- ggplot(soe, aes(x = year)) + 
  geom_line(aes(y = mean_treat), colour="black",linetype = 1,size=1.5) + 
  geom_line(aes(y = lci_treat), colour = "grey",linetype = 1) + 
  geom_line(aes(y = uci_treat), colour = "grey",linetype = 1) + 
  geom_line(aes(y = mean_control), colour="red",linetype = 2,size=1.5) + 
  geom_line(aes(y = lci_control), colour = "pink",linetype = 2) + 
  geom_line(aes(y = uci_control), colour = "pink",linetype = 2) +
  scale_x_continuous(breaks=c(2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,2011,2012))+
  ylab(label="Average Win Rate (%)") + 
  xlab(label="Year")+
  annotate("text", x = 2011.7, y=65, label = "Control Group", size = 4,colour="red",fontface =2)+
  annotate("text", x = 2011.7, y=42, label = "Treatment Group", size = 4,colour="black", fontface =2)+
  ggtitle("SOEs") 


nonsoe <- read.dta("/Users/ywang/Documents/My Documents/Research/working papers/political connections and courts/data/working/CPS relative capture datafile/figures/data/did_yearly_nonsoe.dta")
attach(nonsoe)
names(nonsoe)

plot2 <- ggplot(nonsoe, aes(x = year)) + 
  geom_line(aes(y = mean_treat), colour="black",linetype = 1,size=1.5) + 
  geom_line(aes(y = lci_treat), colour = "grey",linetype = 1) + 
  geom_line(aes(y = uci_treat), colour = "grey",linetype = 1) + 
  geom_line(aes(y = mean_control), colour="red",linetype = 2,size=1.5) + 
  geom_line(aes(y = lci_control), colour = "pink",linetype = 2) + 
  geom_line(aes(y = uci_control), colour = "pink",linetype = 2) +
  scale_x_continuous(breaks=c(2000,2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012))+
  ylab(label="Average Win Rate (%)") + 
  xlab(label="Year")+
  annotate("text", x = 2011, y=75, label = "Treatment Group", size = 4,colour="black",fontface =2)+
  annotate("text", x = 2011, y=20, label = "Control Group", size = 4,colour="red", fontface =2)+
  ggtitle("Non-SOEs") 

require(gridExtra)

grid.arrange(plot1, plot2, ncol=1)

